package treeExercise;


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;



public class SecondExercise {
    static class TreeNode {
        private int val;
        private TreeNode left;
        private TreeNode right;
        public TreeNode() {
        }
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode create(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }
    //最近公共祖先    1
    public TreeNode recentParent(TreeNode root,TreeNode p, TreeNode q){
        if(root == null) return null;
        if(p == root || q == root) return root;
        TreeNode leftTree = recentParent(root.left, p, q);
        TreeNode rightTree = recentParent(root.right, p, q);
        if(leftTree != null && rightTree != null){
            return root;
        } else if(leftTree != null){
            return leftTree;
        } else {
            return rightTree;
        }
    }
    //最近公共祖先    2
    private boolean getPath(TreeNode root, TreeNode p, Stack<TreeNode> stack){
        if(root == null){
            return false;
        }
        stack.push(root);
        if(root == p){
            return true;
        }
        if(getPath(root.left , p ,stack)){
            return true;
        }
        if(getPath(root.right, p , stack)){
            return true;
        }
        stack.pop();
        return false;
    }
    public TreeNode lowestCommomAncerstor(TreeNode root, TreeNode p, TreeNode q){
        Stack<TreeNode> stackp = new Stack<>();
        Stack<TreeNode> stackq = new Stack<>();
        getPath(root, p, stackp);
        getPath(root, q, stackq);
        int lenp = stackp.size();
        int lenq = stackq.size();
        int len = Math.abs(lenp - lenq);
        while (len-- > 0) {
            if (lenp > lenq) {
                stackp.pop();
            } else {
                stackq.pop();
            }
        }
        while(!stackp.isEmpty() && !stackq.isEmpty()){
            if(stackp.peek() == stackq.peek()){
                return stackp.peek();
            }
            stackq.pop();
            stackp.pop();
        }
        return null;
    }
    //通过先序和中序遍历遍历创建二叉树
    private int ipreorder = 0;
    private int find(int[] preorder, int[] inorder, int ibegin, int iend){
        int val = preorder[ipreorder];
        for(int i = 0; i <= inorder.length; i++){
            if(inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTreeChild(int[] preorder, int[] inorder,int ibegin, int iend){
        //判断有无左右子树
        if(ibegin > iend){
            return null;
        }
        //创建根节点
        TreeNode root = new TreeNode(preorder[ipreorder]);
        ipreorder++;
        //从中序遍历中找到根节点下标所在的位置
        int iorder = find(preorder, inorder, ibegin, iend);
        root.left = buildTreeChild(preorder,inorder,ibegin,iorder - 1);
        root.left = buildTreeChild(preorder,inorder,iorder + 1,iend);
        return root;
    }

    //通过二叉树创建字符串
    private StringBuilder s = new StringBuilder();
    public String createString(TreeNode root){
        if(root == null){
            return null;
        }
        s.append(root.val);
        if(root.left != null){
            s.append("(");
            createString(root.left);
            s.append(")");
        } else if(root.right != null){
            s.append(")");
        } else {
            return s.toString();
        }
        if(root.right != null){
            s.append("(");
            createString(root.right);
            s.append(")");
        }
        return s.toString();
    }
    //先序遍历的非递归
    public void preorder(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        stack.push(root);
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                System.out.println(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }
    //中序遍历的非递归

    //后序遍历的非递归
    public List<Integer> postorderTraversal(TreeNode root) {
        if(root == null){
            return null;
        }
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.peek();
            if(top.right == prev || top.right == null){
                list.add(top.val);
                stack.pop();
                prev = top;
            } else {
                cur = top.right;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        SecondExercise test = new SecondExercise();
        TreeNode root = test.create();
        test.postorderTraversal(root);
    }
}
